// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "appid": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "应用唯一标识",
    "label": "应用唯一标识"
  },
  "appname": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "应用名称",
    "label": "应用名称"
  },
  "index_banner": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "banner",
    "label": "banner"
  },
  "index_list": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "首页菜单",
    "label": "首页菜单"
  },
  "notice": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "公告",
    "label": "公告"
  },
  "ads_seat": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "占位广告",
    "label": "占位广告"
  },
  "ads_down": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "下载激励广告",
    "label": "下载激励广告"
  },
  "ads_inter": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "插屏广告",
    "label": "插屏广告"
  },
  "ads_task": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "任务激励广告",
    "label": "任务激励广告"
  },
  "download": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "下载地址",
    "label": "下载地址"
  },
  "task_new": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "任务-新人",
    "label": "任务-新人"
  },
  "task_share": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "任务-分享",
    "label": "任务-分享"
  },
  "h5": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "其他",
    "label": "其他"
  },
  "logs": {
    "rules": [
      {
        "format": "object"
      }
    ],
    "title": "统计日志",
    "label": "统计日志"
  },
  "is_enable": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "是否启用",
    "label": "是否启用"
  }
}

const enumConverter = {}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
